#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2022
@author: GLTS
"""
import sys
import time
import datetime
import win32api
import win32con
import subprocess
from ctypes import windll


def goWechat():
    subprocess.Popen("E:\\Program Files\\Tencent\\WeChat\\WeChat.exe")
    width = windll.user32.GetSystemMetrics(0)
    height = windll.user32.GetSystemMetrics(1)
    print(width, height)
    # 1440 900
    time.sleep(2)
    windll.user32.SetCursorPos(720, 520)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 720, 520)
    time.sleep(0.05)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 720, 520)


def changetext(value1, value2, path):
    with open(path, 'r', encoding='utf-8') as read_temp:
        lines = []
        for line in read_temp.readlines():
            # if line != '\n':
            # lines.append(line)
            lines.append(line)
        read_temp.close()
    with open(path, 'w', encoding='utf-8') as read_temp:
        for line in lines:
            if value1 in line:
                line = value2
                # read_temp.write('%s\n' % line)
                read_temp.write(line + '\n')
            else:
                read_temp.write(line)
        read_temp.close()


date = datetime.datetime.now().strftime('%d')
print('Date today is ' + date)

print('Open file inf.ini')
print('Read file inf.ini and Tidy up information')
file = open("inf.ini", "r", encoding="utf-8")
fileList = file.readlines()
file.close()

list_temp = []
for i in fileList:
    list_temp.append(i.strip('\n'))
fileList = list_temp
print('Information:', end='')
print(fileList)
print('Content in file inf.ini(date.today):' + fileList[0][13:])
print('Content in file inf.ini(open.frequency):' + fileList[1][17:])

if date == fileList[0][13:]:
    print('Today open ' + fileList[1][17:] + ' time')
    if int(fileList[1][17:]) == 0:
        print('Go Wechat')
        goWechat()
        print('Done(Go Wechat)')
        print('Change' + '"open.frequency = 0"' + ' to be ' + '"open.frequency = 1"')
        value1 = 'open.frequency = 0'
        value2 = 'open.frequency = 1'
        path = 'inf.ini'
        changetext(value1, value2, path)
    elif int(fileList[1][17:]) == 1:
        print('quit')
        sys.exit()
elif not date == fileList[0][13:]:
    print('Today open ' + '0' + ' time')
    value1 = fileList[0]
    value2 = 'date.today = ' + date
    path = 'inf.ini'
    changetext(value1, value2, path)
    print('Go Wechat')
    goWechat()
    print('Done(Go Wechat)')
    print('Change' + '"open.frequency = 0"' + ' to be ' + '"open.frequency = 1"')
    value1 = fileList[1]
    value2 = 'open.frequency = 1'
    path = 'inf.ini'
    changetext(value1, value2, path)
